[extern isr_handler]
[extern irq_handler]

[global isr_common_stub]
[global irq_common_stub]

; This is our common ISR stub. It saves the processor state, sets
; up for kernel mode segments, calls the C-level fault handler,
; and finally restores the stack frame.

isr_common_stub:
	pusha			; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax

	mov ax, ds		; Lower 16-bits of eax = ds.
	push eax		; save the data segment descriptor

	mov ax, 0x10	; load the kernel data segment descriptor
	mov ds, ax
	mov es, ax
	mov fs, ax
 	mov gs, ax

	call isr_handler

	pop eax		; reload the original data segment descriptor
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax

	popa			; Pops edi,esi,ebp...
	add esp, 8		; Cleans up the pushed error code and pushed ISR number
	sti
	iret			; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP

irq_common_stub:
	pusha			; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax

	mov ax, ds		; Lower 16-bits of eax = ds.
	push eax		; save the data segment descriptor

	mov ax, 0x10	; load the kernel data segment descriptor
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax

	call irq_handler

	pop ebx		; reload the original data segment descriptor
	mov ds, bx
	mov es, bx
	mov fs, bx
	mov gs, bx

	popa			; Pops edi,esi,ebp...
	add esp, 8		; Cleans up the pushed error code and pushed ISR number
	sti
	iret			; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP

